﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.Linq;
using System.Linq.Expressions;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using YakShaver.WCSF.BuildManager.Interface.Services;
using YakShaver.WCSF.BuildManager.Interface.BusinessEntities;
using YakShaver.WCSF.BuildManager.BusinessEntities;
using Microsoft.Practices.CompositeWeb.ObjectBuilder.Strategies;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
namespace YakShaver.WCSF.BuildManager.Services
{
   public class SandboxService : ISandboxService
    {
        #region ISandboxService Members

       private string _nantExecutable;

       [InjectionConstructor]
       public SandboxService()
       {
           
       }

        public bool Branch(IProjectSandbox sandbox)
        {
            throw new NotImplementedException();
        }

        public bool Merge(IProjectSandbox sandbox)
        {
            throw new NotImplementedException();
        }

        public IProjectSandbox CreateSandbox(IProjectBuild projectBuild, IDictionary<string, object> sandboxProperties, [ServiceDependency] IProjectService projectService)
        {
            string webUrl = sandboxProperties["webUrl"].ToString();
            string sandboxBuildFile = sandboxProperties["sandboxBuildFile"].ToString();
            int commandTimeout = sandboxProperties.ContainsKey("sandboxCommandTimeout") ? (int)sandboxProperties["sandboxCommandTimeout"] : 120000;

            NantCommandLineOptions nantCommandOptions = new NantCommandLineOptions();
            nantCommandOptions.WebUrl = webUrl;
            nantCommandOptions.BuildFile = sandboxBuildFile;
            nantCommandOptions.AdditionalProperties.Add("yakShaver.sourceDirectory", projectService.GetProjectBuildSourceDirectory(projectBuild));

            // now call a nant service to execute the file.
            // possibly kick off a workflow at this point.. but I'm not sure how necessary that is
            // at this stage of the game.

            System.Diagnostics.Process nantProcess = new System.Diagnostics.Process();
            nantProcess.StartInfo.Arguments = nantCommandOptions.ToString();
            nantProcess.StartInfo.CreateNoWindow = true;
            nantProcess.StartInfo.WorkingDirectory = nantCommandOptions.WorkingDirectory;
            nantProcess.StartInfo.FileName = this._nantExecutable;

            nantProcess.Start();

            nantProcess.WaitForExit(commandTimeout);

            if (nantProcess.ExitCode != 0)
            {   //error
            }
            else
            {
            }


            ProjectSandbox sandbox = new ProjectSandbox(projectBuild, webUrl);
            return sandbox;
            
        }

        #endregion

        #region ISandboxService Members


        public bool DestroySandbox(IProjectSandbox sandbox)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ISandboxService Members


        
        #endregion
    }
}
